﻿var SelectedObj;
function InsertInput(type){
    if(SelectedObj){
        if(SelectedObj.tagName=="INPUT"||SelectedObj.tagName=="TEXTAREA"||SelectedObj.tagName=="SELECT"||SelectedObj.tagName=="IMG"){
            var sel=FTB_API[TextArea].GetSelection();
            sel.removeAllRanges();
        };
    };
    if(type=="text"||type=="checkbox"||type=="radio"||type=="button"||type=="file"){
        var str="<input type='"+type+"' name='";
        if(type=="text"){
            str+="Textbox"+GetElementCount(type);
        }else if(type=="checkbox"){
            str+="Checkbox"+GetElementCount(type);
        }else if(type=="radio"){
            str+="Radio"+GetElementCount(type);
        }else if(type=="button"){
            str+="Button"+GetElementCount(type)+"' value='Button";
        }else if(type=="file"){
            str+="File"+GetElementCount(type);
        };
    }else if(type=="textarea"){
        var str="<textarea id='TextArea"+GetElementCount(type)+"' cols='20' rows='2'></textarea>";
        FTB_API[TextArea].InsertHtml(str);
        FTB_API[TextArea].Focus();
        return;
    }else if(type=="select"){
        var str="<select name='Select"+GetElementCount(type)+"'></select>";
        FTB_API[TextArea].InsertHtml(str);
        FTB_API[TextArea].Focus();
        return;
    };
    str+="'>"
    FTB_API[TextArea].InsertHtml(str);
    FTB_API[TextArea].Focus();
};
function Focus(){
    FTB_API[TextArea].Focus();
};
function GetElementCount(type){
    if(type=="text"||type=="checkbox"||type=="radio"||type=="button"||type=="file"){
        var inputs=FTB_API[TextArea].designEditor.document.getElementsByTagName('input');
    }else if(type=="textarea"){
        return FTB_API[TextArea].designEditor.document.getElementsByTagName('textarea').length+1;
    }else if(type=="select"){
        return FTB_API[TextArea].designEditor.document.getElementsByTagName('select').length+1;
    };
    var x=1;
    for(gec=0;gec<inputs.length;gec++){
        if(inputs[gec].type==type){
            x+=1;
        };
    };
    return x;
};
function ShowProperties(e){
    var prop=document.getElementById('Properties');
    for(spi=0;spi<prop.firstChild.rows.length;spi++){
        prop.firstChild.rows[spi].style.display='none';
    };
    var rowID=document.getElementById("rowID");
    var propID=document.getElementById("propID");
    var rowName=document.getElementById("rowName");
    var propName=document.getElementById("propName");
    var rowValue=document.getElementById("rowValue");
    var propValue=document.getElementById("propValue");
    var propChecked=document.getElementById("propChecked");
    var rowChecked=document.getElementById("rowChecked");
    var rowMaxLength=document.getElementById("rowMaxLength");
    var propMaxLength=document.getElementById("propMaxLength");
    var propOptions=document.getElementById("propOptions");
    var rowSelect=document.getElementById("rowSelect");
    var rowSize=document.getElementById("rowSize");
    var propSize=document.getElementById("propSize");
    var rowCols=document.getElementById("rowCols");
    var propCols=document.getElementById("propCols");
    var rowRows=document.getElementById("rowRows");
    var propRows=document.getElementById("propRows");
    obj=e.srcElement;
    if(obj.tagName=="INPUT"){
        if(obj.type=="text"){
            rowName.style.display='block';
            propName.value=obj.name;
            rowID.style.display='block';
            propID.value=obj.id;
            rowValue.style.display='block';
            propValue.value=obj.value;
            rowMaxLength.style.display='block';
            rowMaxLength.value=obj.maxLength;
            rowSize.style.display='block';
            propSize.value=obj.size;
        }else if(obj.type=="checkbox"||obj.type=="radio"){
            rowName.style.display='block';
            propName.value=obj.name;
            rowID.style.display='block';
            propID.value=obj.id;
            rowChecked.style.display='block';
            if(obj.checked==true){propChecked.selectedIndex=0;}else{propChecked.selectedIndex=1;};
        }else if(obj.type=="button"){
            rowValue.style.display='block';
            rowID.style.display='block';
            rowName.style.display='block';
            propValue.value=obj.value;
            propID.value=obj.id;
            propName.value=obj.name;
            rowSize.style.display='block';
            propSize.value=obj.size;
        };
    }else if(obj.tagName=="TEXTAREA"){
        rowName.style.display='block';
        propName.value=obj.name;
        rowID.style.display='block';
        propID.value=obj.id;
        rowValue.style.display='block';
        propValue.value=obj.value;
        rowMaxLength.style.display='block';
        rowMaxLength.value=obj.maxLength;
        rowCols.style.display='block';
        propCols.value=obj.cols;
        rowRows.style.display='block';
        propRows.value=obj.rows;
    }else if(obj.tagName=="SELECT"){
        rowName.style.display='block';
        propName.value=obj.name;
        rowID.style.display='block';
        propID.value=obj.id;
        rowSelect.style.display='block';
        propOptions.innerHTML=obj.innerHTML;
        rowSize.style.display='block';
        propSize.value=obj.size;
    };
    SelectedObj=obj;
};
function UpdateProperty(obj,prop){
    if(prop=="checked"){
        if(obj.selectedIndex==0){
            SelectedObj.checked=true;
        }else{
            SelectedObj.checked=false;
        };
        return;
    };
    SelectedObj[prop]=obj.value;
};
function AddOption(){
    var options=document.getElementById('propOptions');
    var name=document.getElementById('propOptionsName').value;
    var value=document.getElementById('propOptionsValue').value;
    var obj=document.createElement('option');
    obj.innerHTML=name;
    obj.value=value;
    options.appendChild(obj);
    SelectedObj.options[SelectedObj.options.length]=new Option(name,value);
};
function RemoveOption(){
    var select=document.getElementById('propOptions');
    SelectedObj.options.remove(select.selectedIndex);
    select.options.remove(select.selectedIndex);
};
function MoveOption(dir){
    var select=document.getElementById('propOptions');
    var index=select.selectedIndex;
    if((dir=="up"&&index==0)||(dir=="down"&&select.options.length-1==index)||index==-1){return;};
    var arrOptions=new Array();
    for(moi=0;moi<select.options.length;moi++){
        arrOptions[moi]=new Array(select.options[moi].text,select.options[moi].value);
    };
    select.innerHTML="";
    SelectedObj.innerHTML="";
    for(moe=0;moe<arrOptions.length;moe++){
        if(dir=="up"){
            if(moe<index-1||moe>index){
                select.options[select.options.length]=new Option(arrOptions[moe][0],arrOptions[moe][1]);
                SelectedObj.options[SelectedObj.options.length]=new Option(arrOptions[moe][0],arrOptions[moe][1]);
            }else if(moe==index-1){
                select.options[select.options.length]=new Option(arrOptions[index][0],arrOptions[index][1],false,true);
                SelectedObj.options[select.options.length]=new Option(arrOptions[index][0],arrOptions[index][1]);
            }else if(moe==index){
                select.options[select.options.length]=new Option(arrOptions[index-1][0],arrOptions[index-1][1]);
                SelectedObj.options[SelectedObj.options.length]=new Option(arrOptions[index-1][0],arrOptions[index-1][1]);
            };
        }else if(dir=="down"){
            if(moe<index||moe>index+1){
                select.options[select.options.length]=new Option(arrOptions[moe][0],arrOptions[moe][1]);
                SelectedObj.options[select.options.length]=new Option(arrOptions[moe][0],arrOptions[moe][1]);
            }else if(moe==index){
                select.options[select.options.length]=new Option(arrOptions[index+1][0],arrOptions[index+1][1]);
                SelectedObj.options[select.options.length]=new Option(arrOptions[index+1][0],arrOptions[index+1][1]);
            }else if(moe==index+1){
                select.options[select.options.length]=new Option(arrOptions[index][0],arrOptions[index][1],false,true);
                SelectedObj.options[select.options.length]=new Option(arrOptions[index][0],arrOptions[index][1]);
            }; 
        };
    };
};